package com.kanqiubao.kq.fragment;

import android.os.Parcelable;

import java.util.ArrayList;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

public class NewsFragmentStatePagerAdapter extends FragmentPagerAdapter {

    private ArrayList<Fragment> fragments;

    public NewsFragmentStatePagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return ((BaseFragment<?>) fragments.get(position)).getmTitle();
    }

    public NewsFragmentStatePagerAdapter(FragmentManager fm, ArrayList<Fragment> fragments) {
        super(fm);
        this.fragments = fragments;
    }

    public void setData(ArrayList<Fragment> fragments) {
        this.fragments = fragments;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if(fragments!=null){
            return fragments.size();
        }
        return 0;
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    @Override
    public Parcelable saveState() {
        return null;
    }

    public void clear() {
        if (fragments != null) {
            fragments.clear();
            notifyDataSetChanged();
        }

    }
}
